/*
 * @Author: your name
 * @Date: 2021-04-09 06:31:54
 * @LastEditTime: 2021-04-10 13:23:58
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \my-app-koa\client\src\actions\profileActions.js
 */
import { GET_PROFILE, GET_PROFILES, PROFILE_LOADING, GET_ERRORS, CLEAR_CURRENT_PROFILE, SET_CURRENT_USER } from './types'
import axios from 'axios'

export const getCurrentProfile = () => dispatch => {
    dispatch(setProfileLoading())
    axios.get("/api/profile")
        .then(res => dispatch({
            type: GET_PROFILE,
            payload: res.data
        }))
        .catch(err => dispatch({
            type: GET_PROFILE,
            payload: {}
        }))
}

// 根据handle获取个人信息
export const getProfileByHandle = handle => dispatch => {
    // 加载动画
    dispatch(setProfileLoading());
    // 请求数据
    axios(`/api/profile/handle/${handle}`)
        .then(res =>
            dispatch({
                type: GET_PROFILE,
                payload: res.data
            })
        )
        .catch(err =>
            dispatch({
                type: GET_PROFILE,
                payload: null
            })
        )
}


// 获取所有人员信息
export const getProfiles = () => dispatch => {
    // 加载动画
    dispatch(setProfileLoading());
    // 请求所有人信息
    axios.get('/api/profile/all')
        .then(res =>
            dispatch({
                type: GET_PROFILES,
                payload: res.data
            })
        )
        .catch(err =>
            dispatch({
                type: GET_PROFILES,
                payload: null
            })
        )
}


// // 删除个人信息
export const deleteAccout = () => dispatch => {
    axios.delete("/api/profile")
        .then(res => dispatch({
            type: SET_CURRENT_USER,
            payload: {}
        }))
        .catch(err =>
            dispatch({
                type: GET_ERRORS,
                payload: err.response.data
            })
        )
}

export const createProfile = (profileData, history) => dispatch => {
    axios.post("/api/profile", profileData)
        .then(res => history.push("/dashboard"))
        .catch(err =>
            dispatch({ type: GET_ERRORS, payload: err.response.data })
        )
}

export const setProfileLoading = () => {
    return {
        type: PROFILE_LOADING
    }
}

export const clearCurrentProfile = () => {
    return {
        type: CLEAR_CURRENT_PROFILE
    }
}


// 添加个人经历
export const addExperience = (expData, history) => dispatch => {
    axios.post("/api/profile/experience", expData)
        .then(res => history.push("/dashboard"))
        .catch(err =>
            dispatch({
                type: GET_ERRORS,
                payload: err.response.data
            })
        )
}

// 添加教育经历
export const addEducation = (expData, history) => dispatch => {
    axios.post("/api/profile/education", expData)
        .then(res => history.push("/dashboard"))
        .catch(err =>
            dispatch({
                type: GET_ERRORS,
                payload: err.response.data
            })
        )
}

// 删除个人履历
export const deleteExperience = id => dispatch => {
    axios.delete(`/api/profile/experience/${id}`)
        .then(res =>
            dispatch({
                type: GET_PROFILE,
                payload: res.data
            })
        )
        .catch(err =>
            dispatch({
                type: GET_ERRORS,
                payload: err.response.data
            })
        )
}

// 删除教育经历
export const deleteEducation = id => dispatch => {
    axios.delete(`/api/profile/education/${id}`)
        .then(res =>
            dispatch({
                type: GET_PROFILE,
                payload: res.data
            })
        )
        .catch(err =>
            dispatch({
                type: GET_ERRORS,
                payload: err.response.data
            })
        )
}

